package com.jiankangjin.jf360channel.services.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jiankangjin.common.json.jackson.JSONUtil;
import com.jiankangjin.httpclient.HttpClient;
import com.jiankangjin.jf360channel.vo.PayVo;

/**
 * 所有类型版本支付方法实现
 */
public class PayServiceImpl {
	private static final transient Log logger = LogFactory.getLog(PayServiceImpl.class);

	/**
	 * 默认支付 V0.0.1
	 */
	public PayVo default_V0_0_1(String pay_ssn_id, String order_id,
			String pay_fee, String pay_cry, String buyer_token,
			String buyer_voucher, String subjet, String remark,
			String buyer_name, String buyer_email, String seller_name,
			String seller_email, String expand_json) {
		PayVo payVo = new PayVo();
        payVo.setOrder_id(order_id);
        payVo.setPay_ssn_id(pay_ssn_id);
        payVo.setPay_fee(pay_fee);
        payVo.setPay_cry("156");
		// 解开自定义参数
		String accId = null;
		String sessionId = null;
		if (expand_json != null && expand_json.indexOf(":") > 0) {
			@SuppressWarnings("unchecked")
			Map<String, String> customMap = JSONUtil.fromJSON(expand_json, Map.class);
			if (customMap != null) {
				accId = customMap.get("accId");
				sessionId = customMap.get("sessionId");
			}
		}

        payVo.setSub_deal_status("0041");
        payVo.setSub_deal_msg("支付失败");
		if (accId == null || accId.equals("")) {
			payVo.setResp_status("0001");
			payVo.setResp_msg("accId有问题");
			return payVo;
		}
		if (sessionId == null || sessionId.equals("")) {
			payVo.setResp_status("0002");
			payVo.setResp_msg("sessionId有问题");
			return payVo;
		}
/*
		//验证短信是否正确
		Map<String, Object> sPara = new HashMap<String, Object>();
		sPara.put("sessionId", sessionId);
		sPara.put("checkNo", buyer_voucher);
		sPara.put("sign", "transSystem_sign");
		logger.info("验证短信调帐户系统:"+sPara);
		Map<String, Object> accMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/check_sms", sPara);
		logger.info("帐户系统验证短信返回:"+accMap);
		if (accMap == null) {
			logger.error("accmanage返回空，无法验证短信码");
			payVo.setResp_status("0003");
			payVo.setResp_msg("accmanage返回空，无法验证短信码");
			return payVo;
		}
		String accStat = (String) accMap.get("status");
		if ("1001".equals(accStat)) {
			payVo.setResp_status("0004");
			payVo.setResp_msg("短信验证码错误");
			return payVo;
		} else if ("2001".equals(accStat)) {
			payVo.setResp_status("0005");
			payVo.setResp_msg("短信验证码验证失败");
			return payVo;
		} else if ("0001".equals(accStat)) {
			logger.info("短信验证码验证成功");
		}
*/
		Map<String, Object> prams = new HashMap<String, Object>();
		prams.put("userId", buyer_token);
		prams.put("accId", accId);
		prams.put("tradeId", pay_ssn_id.split("-")[0]);
		prams.put("tradeAmount", pay_fee);
		prams.put("appId", "13");
		prams.put("tradeResult", "11");
		prams.put("tradeType", "E");
		logger.info("360渠道调帐户系统交易:"+prams);
		Map<String, Object> acctradeMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/account_trade", prams);
		logger.info("帐户系统返回"+acctradeMap);
		//异常应答
		if(acctradeMap == null) {
			payVo.setResp_status("9999");
			payVo.setResp_msg("帐户系统返回空，无法支付");
			return payVo;
		}
		//正常应答：
		String status = (String) acctradeMap.get("status");
		String payStat = "0041";
		String dealMsg = (String) acctradeMap.get("dealMsg");
		if ("0001".equals(status)) {//返回成功
			payStat = "1001";
			dealMsg = "支付成功";
			payVo.setPay_user(((String) acctradeMap.get("accName")).trim());
			payVo.setPay_fin_time(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
			payVo.setPay_chnl_seq(buyer_token + accId);//记录到数据库，以便退款时使用
		} else if ("3001".equals(status)) {//余额不足
			dealMsg = "余额不足";
		} else if ("2001".equals(status)) {//无此账户
			dealMsg = "无此账户";
		} else if ("1001".equals(status)) {//令牌格式不合法
			dealMsg = "令牌格式不合法";
		}
        payVo.setResp_status("0000");
        payVo.setResp_msg("返回成功");
        payVo.setSign("JF360Channel_public_V3_sign");
        payVo.setSub_deal_status(payStat);
        payVo.setSub_deal_msg(dealMsg);
		return payVo;
	}

}
